package com.skycode7.dao;

import com.skycode7.bean.AdminBean;
import com.skycode7.util.DBConnection;

import java.sql.*;
import java.util.List;

public class AdminDao {
    DBConnection dbc = new DBConnection();
    private List<AdminBean> list;

    /**
     * 添加管理员
     */
    public int add(AdminBean admin) {
        Connection conn = dbc.getconnection();
        String sql = "insert into admin values(null, ?, ?, ?, ?,?)";
        try {
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, (String) admin.getAdminName());
            preparedStatement.setString(2, (String) admin.getAdminPwd());
            preparedStatement.setString(3, String.valueOf(admin.getAdminType()));
            preparedStatement.setString(4, (String) admin.getLoginTime());
            preparedStatement.setInt(5, admin.getAdminID());
            preparedStatement.executeUpdate();
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                System.out.println("");
            }
        }
        return 0;
    }

    public int delect(int AdminID) {
        Connection conn = dbc.getconnection();
        String sql = "delete from admin where AdminID =?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, AdminID);
            return ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println("");
        }
        return 0;
    }

    /**
     *
     */
    public int update(AdminBean admin) {
        Connection conn = dbc.getconnection();
        String sql = "update admin set AdminName =?, AdminPwd =?, AdminType =?, LastLoginTime =? where AdminID =?";
        try {
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, (String) admin.getAdminName());
            preparedStatement.setString(2, (String) admin.getAdminPwd());
            preparedStatement.setString(3, String.valueOf(admin.getAdminType()));
            preparedStatement.setString(4, (String) admin.getLoginTime());
            preparedStatement.setInt(5, admin.getAdminID());
            preparedStatement.executeUpdate();
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                System.out.println("");
            }
        }
        return 0;
    }

    public boolean CheckLogin(String user, String pwd) {
        Connection conn = dbc.getconnection();
        String sql = "select * from admin where AdminName =? and AdminPwd =?";
        try {
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, user);
            preparedStatement.setString(2, pwd);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                return true;
            }
        } catch (SQLException e) {
            System.out.println();
        }
        return false;
    }
}


